<template>
  <div class="AddOrUpdate_main add_container column_three">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="企业编码">
          <el-input v-model="formData.code" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="付款账号">
          <el-input v-model="formData.payAccount" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="企业地址" class="enterpriseLocation">
          <el-input v-model="formData.address" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="企业联系人">
          <el-input v-model="formData.contacts" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="formData.contactsWays" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="联系邮箱">
          <el-input v-model="formData.email" placeholder="请输入" clearable />
        </el-form-item>
        <div class="segmentation" />
        <div class="formItem_header">账号信息</div>
        <el-form-item label="注册人名称" prop="registerName">
          <el-input v-model="formData.registerName" placeholder="请输入" :disabled="isShowRegisterName" clearable />
        </el-form-item>
        <el-form-item label="联系方式" prop="registerPhone">
          <el-input v-model="formData.registerPhone" placeholder="请输入" :disabled="isShowRegisterPhone" clearable />
        </el-form-item>
        <el-form-item label="账号类型" prop="typeId">
          <el-select v-model="formData.typeId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in typeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="二级用户数" prop="secondUserNums">
          <el-select v-model="formData.secondUserNums" filterable placeholder="请选择">
            <el-option
              v-for="item in secondUserNumsOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="永久使用" prop="status">
          <el-radio v-model="formData.foreverUseStatus" label="2">否</el-radio>
          <el-radio v-model="formData.foreverUseStatus" label="1">是</el-radio>
        </el-form-item>
        <el-form-item label="到期日期" :rules="formData.foreverUseStatus == 1 ? [{required: false}]:rules.expirationTime" prop="expirationTime">
          <el-date-picker
            v-model="formData.expirationTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm"
            format="yyyy-MM-dd HH:mm"
            :placeholder="isShowTextExpirationTime"
            :editable="false"
            :disabled="isShowExpirationTime"
          />
        </el-form-item>
        <el-form-item label="租户角色" prop="roleIdsArray" class="double">
          <el-select v-model="formData.roleIdsArray" filterable placeholder="请选择" multiple clearable>
            <el-option
              v-for="item in roleOptions"
              :key="item.roleId+''"
              :label="item.roleName"
              :value="item.roleId+''"
            />
          </el-select>
        </el-form-item>
        <div class="segmentation" />
        <div class="formItem_header">销售信息</div>
        <el-form-item label="对接销售" prop="contacts">
          <el-input v-model="formData.buttSales" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactsWays">
          <el-input v-model="formData.salesContact" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="备注" class="double">
          <el-input v-model="formData.remark" maxlength="200" show-word-limit type="textarea" placeholder="请输入" />
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="formData.status">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdate, getInfo } from '@/api/superManage/tenantManage'
import { listAll } from '@/api/superManage/tenantRole'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    id: {},
    defaultValue: {}
  },
  data() {
    const validateRole = (rule, value, callback) => {
      if (value.length == 0) {
        callback(new Error('租户角色不能为空'))
      } else {
        callback()
      }
    }
    const validate_expirationTime = (rule, value, callback) => {
      if (value && this.$moment(value).isBefore(new Date())) {
        callback(new Error('到期时间不能小于当前时间'))
      } else {
        callback()
      }
    }
    return {
      formData: {
        id: undefined,
        code: undefined,
        name: undefined,
        payAccount: undefined,
        address: undefined,
        contacts: undefined,
        contactsWays: undefined,
        email: undefined,
        registerName: undefined,
        registerPhone: undefined,
        typeId: undefined,
        secondUserNums: 15,
        foreverUseStatus: '2',
        expirationTime: undefined,
        roleIdsArray: undefined,
        buttSales: undefined,
        salesContact: undefined,
        roleIds: undefined,
        remark: undefined,
        status: '1'
      },
      isShowRegisterName: false,
      isShowRegisterPhone: false,
      isShowExpirationTime: false,
      isShowTextExpirationTime: '',
      secondUserNumsOptions: [
        { value: 15, label: '15' },
        { value: 50, label: '50' },
        { value: -1, label: '不限' }
      ],
      roleOptions: [],
      rules: {
        name: [
          { required: true, message: '企业名称不能为空', trigger: 'change' }
        ],
        registerName: [
          { required: true, message: '注册人名称不能为空', trigger: 'change' }
        ],
        registerPhone: [
          { required: true, message: '联系方式不能为空', trigger: 'change' }
        ],
        secondUserNums: [
          { required: true, message: '二级用户数不能为空', trigger: 'change' }
        ],
        typeId: [
          { required: true, message: ' 账号类型不能为空', trigger: 'change' }
        ],
        foreverUseStatus: [
          { required: true, message: ' 永久使用不能为空', trigger: 'change' }
        ],
        roleIdsArray: [
          { required: true, validator: validateRole, trigger: 'change' }
        ],
        expirationTime: [
          { required: true, validator: validate_expirationTime, trigger: 'change' }
        ]
      },
      typeOptions: [
        { value: '1', label: '试用版' },
        { value: '2', label: '正式版' }
      ]
    }
  },
  watch: {
    'formData.foreverUseStatus'() {
      // 到期时间：年月日，永久使用如果选择是，则到期时间不可填写。
      if (this.formData.foreverUseStatus == 1) {
        this.isShowExpirationTime = true
        this.formData.expirationTime = ''
        this.isShowTextExpirationTime = '暂无'
      } else {
        this.isShowExpirationTime = false
        this.isShowTextExpirationTime = '请选择'
      }
    }
  },
  created() {
    this.getSelects()
    if (!this.id) { // 没有id就是新增
      Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
        this.formData[key] = this.defaultValue[key]
      })
    } else { // 有id就是编辑
      this.getInfo()
    }
  },
  methods: {
    getSelects() {
      listAll().then(res => {
        this.roleOptions = res.data
      })
    },
    getInfo() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
          if (!this.formData.foreverUseStatus) {
            this.formData.foreverUseStatus = '2'
          }
        })
        // 注册人名称：必填，账号的名称，首次注册人名称，注册后不可编辑
        if (this.formData.registerName) {
          this.isShowRegisterName = true
        } else {
          this.isShowRegisterName = false
        }
        // 联系方式：必填，登录账号，首次注册人手机号，注册后不可编辑
        if (this.formData.registerPhone) {
          this.isShowRegisterPhone = true
        } else {
          this.isShowRegisterPhone = false
        }
        const foreverUseStatus = this.formData.foreverUseStatus.toString()
        this.formData.foreverUseStatus = foreverUseStatus
        this.formData.roleIdsArray = this.formData.roleIds.split(',')
      })
    },
    onSubmit() {
      if (this.formData.foreverUseStatus == 2 && !this.formData.expirationTime) {
        this.$message({
          message: '到期时间不能为空',
          type: 'warning'
        })
        return
      }
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.formData.roleIds = this.formData.roleIdsArray.join(',')
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
.enterpriseLocation {
  width: calc(100% - 12px);
}
.segmentation {
  width: calc(100% - 40px);
  border-bottom: 1px solid rgb(223,223,223);
  margin-left: 20px;
  margin-bottom: 10px;
}
.formItem_header {
  margin-bottom: 5px;
}
</style>
